<?php
/*
 * add by dragon.hu @2015.10.29  upload file
 */

defined('BASEPATH') or die('forbidden');

class Feedbackreplay_Controller extends Controller {

    private $wholeServerHost;

    function _init() {
        check_level(50, segment(0), segment(1), segment(2, 'index'));
        $config = get_config();
        $this->wholeServerHost = $config['host_url'];
    }

    function index() {
     if (is_submit()) {

         //提交后的逻辑
        switch ($_FILES['upload_file']['error']){
             case UPLOAD_ERR_INI_SIZE:
                 echo '<script>alert("文件大小超过了服务器限制！");</script>';
                 break;
             case UPLOAD_ERR_FORM_SIZE:
                 echo '<script>alert("文件大小超过了服务器限制！");</script>';
                 break;
             case UPLOAD_ERR_PARTIAL:
                 echo '<script>alert("只有部分文件被上传！");</script>';
                 break;
             case UPLOAD_ERR_NO_FILE:
                 echo '<script>alert("没有文件被上传！");</script>';
                 break;
             case UPLOAD_ERR_NO_TMP_DIR:
                 echo '<script>alert("找不到临时文件夹！");</script>';
                 break;
             case UPLOAD_ERR_CANT_WRITE:
                 echo '<script>alert("文件写入失败！");</script>';
                 break;
             case UPLOAD_ERR_OK:
                 //将utf-8编码转成gb2312，以免中文文件名乱码
                 $upload_dir = './'.iconv("UTF-8", "gb2312", $_FILES['upload_file']['name']);

                if (move_uploaded_file($_FILES['upload_file']['tmp_name'], $upload_dir)){
                    //echo '<script>alert("文件上传成功！ ");</script>';
                }else{
                    echo '<script>alert("文件移动失败！ 文件上传失败!");</script>';
                }


                $csvFp = fopen($upload_dir, 'r');

                $is_title_line = true;
                while($csv_line = fgetcsv($csvFp)) {
                    if ($is_title_line == true){
                        $is_title_line = false;
                        continue;
                    }

                    $server_id = $csv_line[0];
                    //echo "表格中的大区ID：  ". $server_id . "   \n\r";
                    $player_id = $csv_line[1];
                    $player_id = trim($player_id);

                    $post_data['SessionID']="ewewqw";
                    $post_data['SessionKey']="wqqew";
                    $post_data['Level'] = 0;
                    $post_data['FightValueL'] = 0;
                    $post_data['FightValueH'] = 0;
                    $post_data['SvrID'] = (int)($server_id);
                    $post_data['TargetIDs'] = array();
                    array_push($post_data['TargetIDs'] , (String)($player_id));

                    $title = $csv_line[3];
                    //$encodeType = mb_detect_encoding($title, array("ASSCII", "GB2312", "GBK", "BIG5"));
                    //$en_title = iconv("UTF-8", "GBK", $title);

                    $msg = $csv_line[4];

                    $post_data['Type'] = 3;
                    $post_data['ItemLst'] = array();

                    $items = array();
                    $items_conf = explode("|", $csv_line[2]);                   
                    $items_conf = trim($items_conf);
                    if($items_conf != ""){
                        foreach ($items_conf as $item_conf) {
                            $item_conf = explode("*", $item_conf);
                            $items[$item_conf[0]]=$item_conf[1];
                        }

                        
                        foreach ($items as $itemid=>$itemnum){
                            $tmpitem=array("ID"=>(int)$itemid,"Num"=>(int)$itemnum);
                            array_push($post_data['ItemLst'] , $tmpitem);
                            $item_nums = $item_nums + 1;
                        }

                        $post_data['Title']=$title;
                        $post_data['Msg'] = $msg;

                        $strResult = vpost($this->wholeServerHost.'send_award_to_player', 3, json_encode($post_data));
                    }else{
                        $post_data['Value']=$title;
                        $post_data['MailContent'] = $msg;
                        $strResult = vpost($this->wholeServerHost.'send_mail_to_player', 3, json_encode($post_data));
                    }

                    $result=json_decode($strResult,true);
                    if($result['RetCode'] == 0)
                    {
                        insert_log(translate('MENU_SEND_OBJECT'),translate('ADD'),NULL,$post_data);
                        //echo "<script>alert('".translate('C_TIPS_SUCCESS_TEXT')."')</script>";
                    }
                    else
                    {
                        echo "<script>alert('".translate('C_TIPS_ERROR_TEXT')."')</script>";
                    }
                }

                fclose($csvFp) or die("can't close file");
                break;
            }
            echo "<script>alert('".translate('C_TIPS_SUCCESS_TEXT')."')</script>";
        }

        $this->view->display("seal/feedbackreplay/index");
    }

    private function upload_file($url,$file_name)
    {
        $post = array(
            "SessionID"=>"ewewqw",
            "SessionKey"=>"wqqew",
            "TbName"=>"@$file_name",//这里是要上传的文件，key与后台处理文件对应
        );

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);

        curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($post));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_VERBOSE, 0);

        $result=curl_exec($ch);
        curl_close($ch);

        return $result;

    }

    function operateDB($path, $username, $passwd)
    {
        $conn = mysql_connect($path, $username, $passwd);
        if (!$conn){
            die('connect failed' . mysql_error());
        }
        mysql_select_db('shouhuzhemen_account', $conn) or die("select database failed" . mysql_error());
        $sql = "UPDATE test SET test1 = 1 WHERE test2 = 2";
        mysql_query($sql);
        mysql_close();
    }
}

?>
